package com.xh.bussiness.course.controller;

import com.xh.bussiness.course.bean.request.CourseRequest;
import com.xh.bussiness.course.service.ICourseService;
import com.xh.bussiness.permission.bean.request.UserRequest;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "课程接口")
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    //旧版需删除
    @PostMapping("/queryByPage")
    @ApiOperation(value = "分页查询课程", notes = "分页查询课程")
    public AjaxResult queryByPage(XhPage page, @RequestBody CourseRequest bean) {
        bean.setUserId(UserUtils.getCurrentUserId());
        bean.setStatus(1);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", courseService.queryPage(page, bean));
    }

    @RequestMapping("/queryByPageV2")
    @ApiOperation(value = "分页查询课程", notes = "分页查询课程")
    public AjaxResult queryByPageV2(XhPage page, CourseRequest bean) {
        bean.setUserId(UserUtils.getCurrentUserId());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", courseService.queryPage(page, bean));
    }

    @PostMapping("/query")
    @ApiOperation(value = "查询课程课次", notes = "查询课程课次")
    public AjaxResult query(HttpServletRequest request,CourseRequest bean) {
        bean.setUserId(request.getHeader("userId"));
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", courseService.query(bean));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加课程", notes = "添加课程")
    public AjaxResult add(@RequestBody CourseRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "添加成功", courseService.add(bean));
    }
    @ExcludeLoginTokenIntercept
    @PostMapping("/modify")
    @ApiOperation(value = "修改课程", notes = "修改课程")
    public AjaxResult modify(@RequestBody CourseRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "更新成功", courseService.modify(bean));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除课程", notes = "删除课程")
    public AjaxResult delete(String id) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "删除成功", courseService.delete(id));
    }

    @PostMapping("/queryNearCourseIndex")
    @ApiOperation(value = "查询最近课程课次", notes = "查询最近课程课次")
    public AjaxResult queryNearCourseIndex(XhPage page, @RequestBody CourseRequest bean) {
        if (StringUtils.isEmpty(bean.getSubjectId())) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误！");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", courseService.queryNearCourseIndex(page, bean));
    }

    @RequestMapping("/queryCourseCount")
    public AjaxResult queryCourseCount(CourseRequest request) {
        if (StringUtils.isEmpty(request.getSubjectId())) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误！");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, courseService.queryCourseCount(request));
    }

    @ApiOperation(value = "根据学科查询课程", notes = "根据学科查询课程")
    @PostMapping("/queryBySubject")
    public AjaxResult queryBySubject(String subject, String name) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", courseService.queryBySubject(subject, name));
    }

    @ApiOperation(value = "发布取消发布接口", notes = "发布取消发布接口")
    @PostMapping("/changePublishStatus")
    public AjaxResult changePublishStatus(String courseId, Integer status) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", courseService.changePublishStatus(courseId, status));
    }

    @PostMapping("/changeCourseSetStep")
    public AjaxResult changeCourseSetStep(String courseId, Integer step) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, courseService.changeCourseSetStep(courseId, step));
    }

}
